package com.yequan.datastructure.base.stack;

import java.util.Collection;
import java.util.LinkedList;

/**
 * @author : Administrator
 * @date : 2020/3/11
 */
public class Stack<T> {
    private LinkedList<T> stack;

    /**
     * 无参构造函数
     */
    public Stack() {
        stack = new LinkedList<T>();
    }

    /**
     * 构造一个包含指定collection中所有元素的栈
     */
    public Stack(Collection<? extends T> c) {
        stack = new LinkedList<T>(c);
    }

    /**
     * 入栈
     * @param t
     */
    public void push(T t) {
        stack.addFirst(t);
    }

    /**
     * 出栈
     * @return
     */
    public T pull() {
        return stack.remove();
    }

    /**
     * 栈是否为空
     * @return
     */
    boolean isEmpty() {
        return stack.isEmpty();
    }

    /**
     * 打印栈元素
     */
    public void show() {
        for (Object o : stack)
            System.out.println(o);
    }

    public static void main(String[] args) {
        Stack<String> stringStack = new Stack<>();
        stringStack.push("1");
        stringStack.push("2");
        stringStack.push("3");
        stringStack.push("4");
        stringStack.show();
    }
}
